package com.gjszn.kh.service;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gjszn.kh.dao.AdminMapper;
import com.gjszn.kh.dao.MemberMapper;
import com.gjszn.kh.model.Admin;
import com.gjszn.kh.model.AdminPO;
import com.gjszn.kh.model.Member;
import com.gjszn.kh.utils.MD5Kit;

/**    
* @Title: AdminService.java  
* @Package com.gjszn.kh.service    
* @author jys@nx-yt.com    
* @date 2015年12月30日 下午5:43:25  
* @version V1.0    
* @Description: TODO(用一句话描述该文件做什么)
********************************************/
@Service("adminService")
public class AdminService {
	
	private Logger log = LogManager.getLogger(AdminService.class);
	
	@Autowired
	private AdminMapper adminMapper;
	
	@Autowired
	private MemberMapper memberMapper;
	
	public int add(Admin admin){
		return adminMapper.insertSelective(admin);
	}
	
	public AdminPO doLogin(String userName, String password){
//		Map<String, String> map = new HashMap<String, String>();
//		map.put("username", userName);
//		map.put("password", password);
		Admin admin = adminMapper.selectForLogin(userName, MD5Kit.getMD5String(password));
		if(admin == null){
			log.info(userName + "登录失败");
			return null;
		}
		AdminPO adminPO = new AdminPO();
		if("admin".equals(admin.getName())){
			adminPO.setNickname(admin.getName());
			adminPO.setFullname("超级管理员");
		}else{
			Member member = memberMapper.selectByPrimaryKey(admin.getName());
			adminPO.setNickname(member.getNumber());
			adminPO.setFullname(member.getName());
		}
		log.info(adminPO.getNickname() + "登录成功");
		return adminPO;
	}
	
	/**
	 * 修改密码
	 * @param name
	 * @param oldPassword
	 * @param newPassword
	 * @return boolean
	 */
	public boolean updatePassword(String name, String oldPassword, String newPassword){
		Admin admin = adminMapper.selectForLogin(name, oldPassword);
		if(admin == null){
			log.info(name + "没有查到数据");
			return false;
		}
		log.info("修改前admin:" + admin);
		admin.setPassword(newPassword);
		if(adminMapper.updateByPrimaryKeySelective(admin) == 1){
			log.info(name + "修改密码成功");
		}else{
			log.info(name + "修改密码失败");
		}
		log.info("修改后admin:" + admin);
		return true;
	}
	
}

